package com.example.demo.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.po.PageRequest;
import com.example.demo.po.PageResult;
import com.example.demo.po.TBizStyle;
import com.example.demo.service.TBizStyleService;
import com.example.demo.util.CodeCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/tBizStyle")
public class TBizStyleController {

    @Autowired
    TBizStyleService tBizStyleService;

    @RequestMapping(value = "/list")
    public ModelAndView list(@ModelAttribute TBizStyle tStyle, HttpServletRequest request, HttpSession session){
        ModelAndView view = new ModelAndView();
        PageRequest page = new PageRequest();
        page.setPageNum(tStyle.getPageNum());
        page.setPageSize(tStyle.getPageSize());
        PageResult resilt = tBizStyleService.findByPage(tStyle,page);
        view.setViewName("/style/style_list");
        view.addObject("result", JSON.toJSON(resilt));
        view.addObject("level",tStyle.getLevel());
        return  view;
    }

    @RequestMapping(value = "/infoList")
    public ModelAndView infoList(@ModelAttribute TBizStyle tStyle, HttpServletRequest request, HttpSession session){
        ModelAndView view = new ModelAndView();
        PageRequest page = new PageRequest();
        page.setPageNum(tStyle.getPageNum());
        page.setPageSize(tStyle.getPageSize());
        PageResult resilt = tBizStyleService.findByPage(tStyle,page);
        view.setViewName("/style/style_infoList");
        view.addObject("result", JSON.toJSON(resilt));
        view.addObject("level",tStyle.getLevel());
        return  view;
    }

    @RequestMapping(value = "/infoList1")
    public ModelAndView infoList1(@ModelAttribute TBizStyle tStyle, HttpServletRequest request, HttpSession session){
        ModelAndView view = new ModelAndView();
        PageRequest page = new PageRequest();
        page.setPageNum(tStyle.getPageNum());
        page.setPageSize(tStyle.getPageSize());
        PageResult resilt = tBizStyleService.findByPage(tStyle,page);
        view.setViewName("/style/style_info1List");
        view.addObject("result", JSON.toJSON(resilt));
        view.addObject("level",tStyle.getLevel());
        return  view;
    }

    @RequestMapping(value = "/saveInit")
    public ModelAndView saveInit(@ModelAttribute TBizStyle tStyle,HttpServletRequest request, HttpSession session){
        ModelAndView view = new ModelAndView();
        System.out.println(tStyle.getId());
        if(tStyle.getId()!=null&&!"".equals(tStyle.getId())){
            TBizStyle styleInfo = tBizStyleService.getById(tStyle.getId());
            view.addObject("tStyle",styleInfo);
        }
        if(tStyle.getLevel()!=null){
            List<TBizStyle> fStyleInfo = tBizStyleService.findByParams(tStyle);
            view.addObject("styleList",fStyleInfo);
        }
        if(tStyle.getLevel()!=null && tStyle.getLevel()==1)
            view.addObject("level",tStyle.getLevel()+1);
        view.setViewName("/style/style_save");
        return  view;
    }


    @RequestMapping(value = "/saveOrUpdate",method = RequestMethod.POST)
    public String saveOrUpdate(@RequestBody TBizStyle tStyle, HttpServletRequest request, HttpSession session) {
        if(tBizStyleService.saveOrUpdate(tStyle,request)){
            CodeCache ca = new CodeCache();
            List<TBizStyle> list = tBizStyleService.findByParams(new TBizStyle());
            ca.freshen(list);
            return "success";
        }
        else
            return "false";
    }

    @RequestMapping(value = "/del" ,method = RequestMethod.GET)
    public String del(@RequestParam(value = "id") Integer id, HttpServletRequest request, HttpSession session){
        JSONObject jsonObject = new JSONObject();
        try{
            if(tBizStyleService.del(id)){
                CodeCache ca = new CodeCache();
                List<TBizStyle> list = tBizStyleService.findByParams(new TBizStyle());
                ca.freshen(list);
                jsonObject.put("oper_res","success");
            }else{
                jsonObject.put("oper_res",false);
            }
        }catch (Exception e){
            e.printStackTrace();
            jsonObject.put("oper_res",false);
        }
        return  jsonObject.toJSONString();
    }


}
